export default {
    dataURLtoFile: function (dataurl, filename){//将base64转换为文件
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n)
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], filename, {type: mime})
    },

    getRandom: function(){
        var ran = Math.random() + ''
        return ran.replace('.', '')
    },
    fileToUrl: function(file){
        return new Promise((resolve, reject) => {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload =  (e) => {
                
                    var dataUrl = reader.result
                    resolve(dataUrl)
                   
            }
        })
    },
    urlToFile ({url, width, height}) {
        var self = this
        return new Promise((resolve, reject) => {
            var img = new Image();
            // img.crossOrigin = 'anonymous'
            
            img.onload = () => {
              
                var imgWidth = img.width
                var imgHeight = img.height
                var isCut = false
               
                if (imgWidth > width || imgHeight > height) {
                    isCut = true
                }
                if (isCut) {
                    var rotoImg = imgWidth / imgHeight
                    var rotoCanvas = width / height
                    if (rotoImg >= rotoCanvas) {
                        var newWidth = width
                        var newHeigth = (newWidth /  imgWidth) * imgHeight
                    } else {
                        var newHeigth = height
                        var newWidth = (newHeigth /  imgHeight) * imgWidth
                    }
    
                    
                } else {
                    var newWidth = imgWidth
                    var newHeigth = imgHeight
                }
                try {
                  
                    var canvas = document.createElement('canvas')
                    var context = canvas.getContext('2d')
                    canvas.width = newWidth
                    canvas.height = newHeigth
                    context.clearRect(0, 0, newWidth, newHeigth)
                    
                    context.drawImage(img, 0, 0, newWidth, newHeigth)
                   
                    var dataURL = canvas.toDataURL("image/jpeg", 1.0)
                   
                    var compressFile = self.dataURLtoFile(dataURL, self.getRandom() + '.jpg')
                   
                    resolve(compressFile)
                } catch (error) {
                    alert(JSON.stringify(error))
                    reject()
                }
               
                
            }
            img.src = url;
           
        })
    },

    async compressFile({file, width, height}){
        let url = await this.fileToUrl(file)
        let pressFile = await this.urlToFile({url, width, height})
        return pressFile
    }
}
